package LinkedList;

public class MyLinkedList implements IList{


    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;


    @Override
    public void addFirst(int data) {
        ListNode Node=new ListNode(data);
        if(head==null){
            head=Node;
            last=Node;
        }else{
            Node.next=head;
            head.prev=Node;
            head=Node;
        }

    }

    @Override
    public void addLast(int data) {

        ListNode Node=new ListNode(data);
        if(head==null){
            head=Node;
            last=Node;
        }else {
            Node.prev=last;
            last.next=Node;
            last=Node;
        }

    }

    public ListNode indexInsert(int index){
        ListNode cur=head;
        while(index!=0){
            cur=cur.next;
            index--;

        }
        return cur;

    }
    /*
    * 指定位置插入
    * */
    @Override
    public void addIndex(int index, int data) {
        if(index<0&&index>size()-1){
            throw new indexException("下标异常"+index);
        }
        if(index==0){//头插法
            addFirst(data);
            return;
        }
        if(index>size()-1){//尾插法
            addLast(data);
            return;
        }
        ListNode Node=new ListNode(data);
        ListNode cur=indexInsert(index);//将要要插入的位置
        Node.next=cur;
        Node.prev=cur.prev;
        cur.prev.next=Node;
        cur.prev=Node;//


    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;

        while(cur!=null){
            if(cur.val!=key){
                cur=cur.next;

            }else {
                return true;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {

        ListNode cur=head;
        if(cur==null){
            return;
        }
        while (cur!=null){
            if(cur.val!=key){
                cur=cur.next;
            }else{
                if(cur==head ){//删除头节点
                    head=head.next;
                    if(head==null){//判断只有一个节点
                        last=null;
                        return;
                    }
                    head.prev=null;//
                    return;
                }else{
                    if(cur==last){//删除尾节点
                        last=last.prev;
                        last.next=null;
                        return;
                    }
                    //删除中间元素
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev.next;

                }
                return;

            }
        }

        System.out.println("未找到值key:"+key);

    }


    @Override
    public void removeAllKey(int key) {


        ListNode cur=head;
        if(cur==null){
            return;
        }
        while (cur!=null){
            if(cur.val!=key){
                cur=cur.next;
            }else{
                if(cur==head ){//删除头节点
                    head=head.next;
                    if(head==null){//判断只有一个节点
                        last=null;
                    }
                    head.prev=null;//头节点的前驱为零
                    cur=head;//头节点删除后须让cur后移
                }else{
                    cur.prev.next = cur.next;
                    if(cur==last){//删除尾节点
                        last=last.prev;
                    }else {
                        //删除中间元素
                        cur.next.prev = cur.prev;
                    }
                    cur=cur.next;//删除后后移cur
                }

            }
        }

    }

    @Override
    public int size() {
        int count=0;
        ListNode cur=this.head;
        while (cur!=null){
            count++;
            cur=cur.next;
        }

        return count;
    }

    @Override
    public void clear() {
        head=null;
        last=null;
    }
/*
* 打印链表
* */
    @Override
    public void display() {
        ListNode cur=this.head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
}
